package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.NurDeatialAdapter;
import com.dexilaonian.bean.NurDetailtBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.view.DividerItemDecoration;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/4/11.
 */

public class NuringDetailActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_title_service)
    TextView baseTitleService;
    @Bind(R.id.bg_person)
    ImageView bgPerson;
    @Bind(R.id.iv_nuring_person)
    ImageView ivNuringPerson;
    @Bind(R.id.tv_person_name)
    TextView tvPersonName;
    @Bind(R.id.tv_person_profession)
    TextView tvPersonProfession;
    @Bind(R.id.nuring_address)
    TextView nuringAddress;
    @Bind(R.id.nuring_service_time)
    TextView nuringServiceTime;
    @Bind(R.id.introdece_more)
    TextView introdeceMore;
    @Bind(R.id.person_introduce)
    TextView personIntroduce;
    @Bind(R.id.btn_nuring)
    Button btnNuring;
    @Bind(R.id.servic_nuring_recyclerview)
    RecyclerView servicNuringRecyclerview;
    private NurDeatialAdapter yiyangAdapter;
    private ArrayList<NurDetailtBean> nuringDetailBeen;
    private String nur_rul = Constant.base_url + "/serviceuser/getProjectsOfServiceUser";
    private String serviceuserid;
    private String content_time;
    private String id1;
    private String introduce;
    private String name;
    private String pic_url;
    private String price22;
    private int price;

    @Override
    public int getLayoutID() {
        return R.layout.activity_nuringdetail;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        baseTitleService.setText("人员详情");
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String level_name = extras.getString("level_name");
        String address = extras.getString("address");
        String name = extras.getString("name");
        String pic_url = extras.getString("pic_url");
        String introduce = extras.getString("introduce");
        String time = extras.getString("TIME");
        serviceuserid = extras.getString("id3");
        tvPersonName.setText(name);
        tvPersonProfession.setText(level_name);
        nuringServiceTime.setText("已服务"+time+"次");
        personIntroduce.setText(introduce);
        nuringAddress.setText(address);

        initGetDetail();
    }

    private void initGetDetail() {
        showProgressBar(true);

        Map<String, String> map = new HashMap<>();
        map.put("serviceuserid", serviceuserid + "");
        map.put("index", 1 + "");
        map.put("pageSize", 10 + "");
        Okhttp.postString(false, nur_rul, map, new Okhttp.CallBac() {



            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("TTTTinitGetDetail", response);
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(NuringDetailActivity.this, LoginActivity.class));
                        return;
                    }
                    String data = jsonObject.optString("data");
                    JSONObject object = new JSONObject(data);
                    String items = object.optString("items");
                    nuringDetailBeen = JsonUtil.json2beans(items, NurDetailtBean.class);

                    List<NurDetailtBean> nuringlist = new ArrayList<NurDetailtBean>();
                    nuringlist.clear();
                    nuringlist.addAll(nuringDetailBeen);
                    Log.e("TTTlistliat", nuringlist.size() + "====="+response);
                    yiyangAdapter = new NurDeatialAdapter(NuringDetailActivity.this, nuringlist);
                    servicNuringRecyclerview.setLayoutManager(new LinearLayoutManager(NuringDetailActivity.this));
                    servicNuringRecyclerview.setAdapter(yiyangAdapter);

                    //添加分割线
                    servicNuringRecyclerview.addItemDecoration(new DividerItemDecoration(NuringDetailActivity.this,
                            DividerItemDecoration.VERTICAL_LIST));

                    yiyangAdapter.setItemClickListener(new NurDeatialAdapter.MyItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                         /* Intent intent = new Intent(NuringDetailActivity.this, ConfirmOrderActivity.class);
                            int id3 = nuringDetailBeen.get(position).getId();
                            Bundle bundle = new Bundle();
                            bundle.putInt("IDDNN22", id3);
                            // intent.putExtras(bundle);
                            intent.putExtras(bundle);
                            startActivity(intent);*/
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
            }
        });
    }

    @OnClick({R.id.btn_nuring,R.id.back, R.id.iv_nuring_person, R.id.introdece_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.iv_nuring_person:
                break;
            case R.id.introdece_more:
                showToast("目前没有更多");
                break;
            case R.id.btn_nuring:
                for (int i = 0; i < nuringDetailBeen.size(); i++) {
                    content_time = String.valueOf(nuringDetailBeen.get(i).getContent_time());
                    id1 = String.valueOf(nuringDetailBeen.get(i).getId());
                    introduce = nuringDetailBeen.get(i).getIntroduce();
                    name = nuringDetailBeen.get(i).getName();
                    pic_url = nuringDetailBeen.get(i).getPic_url();
                    price22 = nuringDetailBeen.get(i).getPrice();
                    price = (int)Double.parseDouble(price22);
                }
                Intent intent = new Intent(NuringDetailActivity.this, ConfirmOrderActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("PRICE",price);
               // bundle.putInt("customer_id",0);
                intent.putExtras(bundle);
                intent.putExtra("mContent_time", content_time);
                intent.putExtra("mNotice", introduce);
                intent.putExtra("mContent_method", name);
                intent.putExtra("projectid", id1);
                intent.putExtra("serviceuserid", serviceuserid);
                intent.putExtra("pic_url", pic_url);
                intent.putExtra("CONFIRM2", "");
                startActivity(intent);
                break;
        }
    }

}
